package com.wn.cl.park.config;

import com.wn.cl.park.mapper.*;
import com.wn.cl.park.model.entity.*;
import com.wn.cl.park.model.enums.DmhStatus;
import com.wn.cl.park.model.enums.DmhStatusString;
import com.wn.cl.park.until.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
public class DelayedMessageListener {

    @Autowired
    private PaymentsMapper paymentsMapper;

    @Autowired
    private ReservationsMapper reservationsMapper;

    @Autowired
    private ParkingSpotsMapper parkingSpotsMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private SmsUtil smsUtil;

    //监听超时未支付
    @RabbitListener(queues = "unpaid-queue")
    @Transactional
    public void handleDelayedMessage(Long payId) {
        // 根据订单ID查询支付记录的状态
        Reservations reservations = reservationsMapper.selectById(payId);

        if (reservations != null && DmhStatus.RESERVATIONS_LOCK.getCode().equals(reservations.getStatus())) {


            // 预约表改为未支付取消
            reservations.setStatus(DmhStatus.RESERVATIONS_NO_PAY.getCode());
            reservationsMapper.updateById(reservations);

            // redis存储取消次数
            String key = DmhStatusString.OVERDUE.getConstant() + reservations.getUserId();
            Boolean isSet = redisTemplate.opsForValue().setIfAbsent(key, 1, 60, TimeUnit.MINUTES);
            if(!isSet){
                redisTemplate.opsForValue().increment(key);
            }



            //车位表改为未占用
            ParkingSpots parkingSpots = parkingSpotsMapper.selectById(reservations.getParkingSpotId());
            parkingSpots.setStatus(DmhStatus.SPOT_NO_RECEIVE.getCode());
            parkingSpotsMapper.updateById(parkingSpots);

            //订单表改为取消
            Order order = orderMapper.selectById(reservations.getOrderId());
            order.setState(DmhStatus.ORDER_PAY_CANCEL.getCode());

        }
    }


    //监听发送消息
    @RabbitListener(queues = "message-queue")
    public void handleMessage(Map<String, Object> message) {
        Long userIdStr = (Long) message.get("userId");
        Long reservationId = (Long) message.get("reservationId");
        Reservations reservations = reservationsMapper.selectById(reservationId);
        if(reservations.getStatus()==DmhStatus.RESERVATIONS_PAY.getCode()){
            Users users = usersMapper.selectById(userIdStr);
//            smsUtil.sendSms(users.getPhoneNumber(), "离您预定的停车时间还剩5分钟结束");
        }
    }

    //监听超时未入场
    @RabbitListener(queues = "timeout-queue")
    public void handleTimeoutMessage(Long reservationId) {
        Reservations reservations = reservationsMapper.selectById(reservationId);
        if(Objects.equals(reservations.getStatus(), DmhStatus.RESERVATIONS_PAY.getCode())){
            reservations.setStatus(DmhStatus.RESERVATIONS_TIMEOUT.getCode());
            reservationsMapper.updateById(reservations);
            ParkingSpots parkingSpots = parkingSpotsMapper.selectById(reservations.getParkingSpotId());
            parkingSpots.setStatus(DmhStatus.SPOT_NO_RECEIVE.getCode());
            parkingSpotsMapper.updateById(parkingSpots);
        }
    }


}